package com.qipay.sp.excutor.uline;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.SignSortMD5Utils;
import com.qipay.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class ULineAlipayTradeQueryExcutor extends AbstractExcutor {

    private final static String KEY_NAME="key";
    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_MCHID_NAME="mch_id";
    private final static String SIGN_NAME="sign";
//    private final static String API_URL="http://api.stage.uline.cc/alipay/orders/query";
    private final static String API_URL="http://api.cmbxm.mbcloud.com/alipay/orders/query";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_TRADE_QUERY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("out_trade_no", order.getTradeNo());
        apiParams.put("nonce_str", new Date().getTime());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, ULineAlipayTradeQueryExcutor.CONFIG_KEY_NAME);
        Object mchId = getVal(spconfig, ULineAlipayTradeQueryExcutor.CONFIG_MCHID_NAME);

        params.put("mch_id", mchId);

        String sign = SignSortMD5Utils.getSign(params,
                key.toString(),
                ULineAlipayTradeQueryExcutor.SIGN_NAME,
                ULineAlipayTradeQueryExcutor.KEY_NAME);

        params.put(ULineAlipayTradeQueryExcutor.SIGN_NAME, sign);

        String result = sendPost(params, ULineAlipayTradeQueryExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求uline的支付宝查询接口失败");
        }
        Map<String, Object> responseMap = XmlUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || responseMap.get("return_code") == null){
            logger.error("请求uline的支付宝查询接口失败, 无法序列化:" + result);
            return null;
        }

        if(!StringUtils.equalsIgnoreCase("SUCCESS", responseMap.get("return_code").toString())){
            logger.error("请求uline的支付宝查询接口失败:" + result);
            return null;
        }

        if(!isResponseValid(responseMap, key.toString(), ULineAlipayTradeQueryExcutor.SIGN_NAME,
                ULineAlipayTradeQueryExcutor.KEY_NAME)) {
            logger.error("请求uline的支付宝查询接口返回值的签名失败:" + result);
            return null;
        }

        if(!"SUCCESS".equalsIgnoreCase(responseMap.get("result_code").toString()))
        {
            logger.error("请求uline的支付宝查询接口业务执行失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("err_code", responseMap.get("err_code"));
            resultMap.put("err_msg", responseMap.get("err_code_des"));
            return resultMap;
        }
        resultMap.put("result_code", "0");

        resultMap.put("trade_state", responseMap.get("trade_state"));
        resultMap.put("trade_no", responseMap.get("out_trade_no"));
        resultMap.put("out_transaction_id", responseMap.get("out_transaction_id"));
        resultMap.put("total_fee", responseMap.get("total_fee"));
        resultMap.put("time_end", responseMap.get("time_end"));
        return resultMap;
    }
}
